package com.secsys.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.secsys.model.TbSysLog;
import com.secsys.model.TbSysResource;
import com.secsys.model.TbSysRole;
import com.secsys.model.TbSysRoleResource;
import com.secsys.model.TbSysUser;
import com.secsys.service.impl.LogServiceImpl;
import com.secsys.service.impl.ResourceServiceImpl;
import com.secsys.service.impl.RoleResourceServiceImpl;
import com.secsys.service.impl.RoleServiceImpl;
import com.secsys.service.impl.UserServiceImpl;

public class LogAction extends BaseAction<TbSysLog>{

	@Override
	public TbSysLog getModel() {
		// TODO Auto-generated method stub
		return null;
	}
	
	private RoleServiceImpl roleService;
	
	private ResourceServiceImpl resourceService;
	
	private RoleResourceServiceImpl roleResourceService;
	
	private UserServiceImpl userService;
	
	private LogServiceImpl logService;
	
	private List<TbSysLog> logList;
	
	private TbSysLog sysLog;
	
	private Integer logId;
	
	/**
	 * 查找出日志列表
	 * @return
	 */
	public String listLog(){
		
		logList = logService.listLog();
		return "listLog";
	}
	
	/**
	 * 查看详细工作日志
	 * @return
	 */
	public String showLog(){
		
		sysLog = logService.findById(logId);
		return "showLog";
	}
	
	/**
	 * 恢复日志
	 * @return
	 */
	public String regainLog(){
		String operation = sysLog.getOperation();
		System.out.println(operation);
		if(operation.contains("添加") && operation.contains(" 角色")){
			if(operation.contains("资源")){//为。。。角色添加。。资源的回滚方法
				roleResourceService.deleteById(Integer.parseInt(sysLog.getRemark()));//删除该角色所属资源
				sysLog.setLogFlag(1);
				logService.saveOrUpdate(sysLog);
			}else{//添加角色的回滚方法
				roleService.deleteById(Integer.parseInt(sysLog.getRemark()));//删除角色
				sysLog.setLogFlag(1);
				logService.saveOrUpdate(sysLog);
			}
			
		}else if(operation.contains("修改")){
			if(operation.contains("用户。")){//修改用户的回滚方法
				updateUser(sysLog);
				
			}else if(operation.contains("角色。")){//修改角色的回滚方法
				
			}else if(operation.contains("资源。")){//修改资源的回滚方法
				updateResource(sysLog);
			}
		}else if(operation.contains("添加") && operation.contains("资源") && !operation.contains("角色")){//添加资源的回滚方法
			resourceService.deleteById(Integer.parseInt(sysLog.getRemark()));//删除资源
			sysLog.setLogFlag(1);
			logService.saveOrUpdate(sysLog);
		}else if(operation.contains("添加") && operation.contains("用户。")){//添加用户回滚方法
			userService.deleteById(Integer.parseInt(sysLog.getRemark()));
			sysLog.setLogFlag(1);
			logService.saveOrUpdate(sysLog);
		}else if(operation.contains("删除")){
			if(operation.contains("角色") && operation.contains("资源")){//为。。。角色删除。。资源的回滚方法
				deleteResourceOfRole(sysLog);
			}else if(operation.contains("角色") && !operation.contains("资源")){//删除角色的回滚方法
				updateRole(sysLog);
			}else if(operation.contains("资源") && !operation.contains("角色")){//删除资源的回滚方法
				deleteResource(sysLog);
			}
		}
		request.put("successMsg",sysLog.getOperation() + " 的工作日志恢复成功");
		request.put("successUrl", "/OneSecSys/bbs/log/log_listLog.action");
		return "successMsg";
	}
	
	/**
	 * 修改用户的回滚方法
	 * @param sysLog
	 */
	private void updateUser(TbSysLog sysLog) {
		try {
		DateFormat format2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String remark = sysLog.getRemark();
		String remarkTest[] = remark.split(",");
		TbSysUser userLog = new TbSysUser();
		userLog.setId(Integer.parseInt(remarkTest[0]));
		userLog.setUserName(remarkTest[1]);
		userLog.setEmail(remarkTest[2]);
		userLog.setMsn(remarkTest[4]);
		userLog.setQq(remarkTest[5]);
		userLog.setSex(remarkTest[6]);
		if(remarkTest[7]!=null && !"".equals(remarkTest[7])){			
			userLog.setBrithday(format2.parse(remarkTest[7]));
		}
		userLog.setResume(remarkTest[8]);
		userLog.setCompany(remarkTest[9]);
		userLog.setCareer(remarkTest[10]);
		userLog.setProvince(remarkTest[11]);
		userLog.setCity(remarkTest[12]);
		userLog.setLevel(Integer.parseInt(remarkTest[13]));
		if(remarkTest[14]!=null && "".equals(remarkTest[14])){
			userLog.setTbSysRole(roleService.findById(Integer.parseInt(remarkTest[14])));
		}
		userLog.setPassword(remarkTest[15]);
		userLog.setOnlineStatue(remarkTest[16]);
		userLog.setRegisterAddr(remarkTest[17]);
		if(remarkTest[18]!=null && !"".equals(remarkTest[18])){
			userLog.setRegisterTime(format2.parse(remarkTest[18]));
		}
		userLog.setUserStatue(remarkTest[19]);
		
		userService.saveOrUpdate(userLog);
		
		sysLog.setLogFlag(1);
		logService.saveOrUpdate(sysLog);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改角色的回滚方法
	 * @param sysLog
	 */
	private void updateRole(TbSysLog sysLog){
		String remark = sysLog.getRemark();
		String remarkTest[] = remark.split(",");
		TbSysRole roleLog = new TbSysRole();
		roleLog.setId(Integer.parseInt(remarkTest[0]));
		roleLog.setRole(remarkTest[1]);
		roleLog.setStatue(remarkTest[2]);
		roleLog.setRemark(remarkTest[3]);
		roleService.saveOrUpdate(roleLog);
		sysLog.setLogFlag(1);
		logService.saveOrUpdate(sysLog);
	}
	
	/**
	 * 修改资源的回滚方法
	 * @param sysLog
	 */
	private void updateResource(TbSysLog sysLog){
		String remark = sysLog.getRemark();
		String remarkTest[] = remark.split(",");
		TbSysResource resource = new TbSysResource();
		resource.setId(Integer.parseInt(remarkTest[0]));
		resource.setMenuName(remarkTest[1]);
		resource.setSequenceNum(remarkTest[2]);
		resource.setUri(remarkTest[3]);
		resource.setParentFlag(remarkTest[4]);
		resourceService.saveOrUpdate(resource);
		sysLog.setLogFlag(1);
		logService.saveOrUpdate(sysLog);
	}
	
	/**
	 * 删除资源的回滚方法
	 * @param sysLog
	 */
	private void deleteResource(TbSysLog sysLog){
		String remark = sysLog.getRemark();
		String remarkTest[] = remark.split(",");
		TbSysResource resource = new TbSysResource();
		resource.setMenuName(remarkTest[0]);
		resource.setSequenceNum(remarkTest[1]);
		resource.setUri(remarkTest[2]);
		resource.setParentFlag(remarkTest[3]);
		resourceService.addResource(resource);//恢复删除资源
		TbSysResource resourceLog = resourceService.findTbSysResourceByMenuName(remarkTest[0]);
		TbSysRoleResource roleResourceLog = new TbSysRoleResource();
		for(int i=4;i<remarkTest.length;i++){//恢复该资源所属的角色
			System.out.println(remarkTest[i]);
			roleResourceLog.setTbSysResource(resourceLog);
			roleResourceLog.setTbSysRole(roleService.findById(Integer.parseInt(remarkTest[i])));
			roleResourceService.save(roleResourceLog);
		}
		sysLog.setLogFlag(1);
		logService.saveOrUpdate(sysLog);
	}
	
	/**
	 * 为。。。角色删除。。资源的回滚方法
	 * @param sysLog
	 */
	private void deleteResourceOfRole(TbSysLog sysLog){
		String remark = sysLog.getRemark();
		String remarkTest[] = remark.split(",");
		TbSysRoleResource roleResourceLog = new TbSysRoleResource();
		roleResourceLog.setTbSysResource(resourceService.findById(Integer.parseInt(remarkTest[0])));
		roleResourceLog.setTbSysRole(roleService.findById(Integer.parseInt(remarkTest[1])));
		roleResourceService.save(roleResourceLog);
		sysLog.setLogFlag(1);
		logService.saveOrUpdate(sysLog);
		
	}

	public RoleServiceImpl getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleServiceImpl roleService) {
		this.roleService = roleService;
	}

	public ResourceServiceImpl getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceServiceImpl resourceService) {
		this.resourceService = resourceService;
	}

	public RoleResourceServiceImpl getRoleResourceService() {
		return roleResourceService;
	}

	public void setRoleResourceService(RoleResourceServiceImpl roleResourceService) {
		this.roleResourceService = roleResourceService;
	}

	public LogServiceImpl getLogService() {
		return logService;
	}

	public void setLogService(LogServiceImpl logService) {
		this.logService = logService;
	}


	public List<TbSysLog> getLogList() {
		return logList;
	}


	public void setLogList(List<TbSysLog> logList) {
		this.logList = logList;
	}


	public UserServiceImpl getUserService() {
		return userService;
	}


	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

	public Integer getLogId() {
		return logId;
	}

	public void setLogId(Integer logId) {
		this.logId = logId;
	}

	public TbSysLog getSysLog() {
		return sysLog;
	}

	public void setSysLog(TbSysLog sysLog) {
		this.sysLog = sysLog;
	}
	

}
